import request from '@/utils/request.js'

// 导航列表请求接口
// https://m.vipmro.com/emro_interface/index/homeNavigation?t=1663054295647&platform=5&channel=2

const state = {
	navList:[],
	bannerInfo:{},
	menuList:[],
	appreciine:{},
	bigTrademark:{},
	mapList:[],
	homeGoodsList:[],
}
const mutations = {
	SET_NAVLIST(state,navList){
		state.navList = navList
	},
	SET_BANNERTLIST(state,bannerInfo){
		state.bannerInfo = bannerInfo
	},
	SET_MENULIST(state,menuList){
		state.menuList = menuList
	},
	SET_APPRECIINE(state,appreciine){
		state.appreciine = appreciine
	},
	SET_BIGTRADEMARK(state,bigTrademark){
		state.bigTrademark = bigTrademark
	},
	SET_MAPLIST(state,mapList){
		state.mapList = mapList
	},
	SET_HOMEGOODSLIST(state,homeGoodsList){
		state.homeGoodsList = homeGoodsList
	}
}

const actions = {
	// 获取nav列表
	async getNavList({commit}){
		try{
			const result = await request('/index/homeNavigation?t=1663054295647&platform=5&channel=2')
			commit('SET_NAVLIST',result.data.list)
		}catch(e){
			console.log(e)
		}
	},
	// 获取banner列表
	async getBannerInfo({commit}){
		try{
			const result = await request('/adver/appIndexAds?positions=1%2C40%2C31&t=1663060275175&platform=5&channel=2')
			commit('SET_BANNERTLIST',result.data)
		}catch(e){
			console.log(e)
		}
	},
	// 获取菜单列表
	async getMenuList({commit}){
		try{
			const result = await request('/index/m/getMallFunctionIcons?t=1663060275175&platform=5&channel=2')
			// console.log(result.data.functionIcons)
			commit('SET_MENULIST',result.data.functionIcons)
		}catch(e){
			console.log(e)
		}
	},
	
	// 获取抢购数据
	async getAppreciine({commit}){
		try{
			const result = await request('/apphome/appHomeActivityNew?t=1663060275176&platform=5&channel=2')
			// console.log(result.data)
			commit('SET_APPRECIINE',result.data)
		}catch(e){
			console.log(e)
		}
	},
	// 获取大牌数据
	async getTradmark({commit}){
		try{
			const result = await request('/apphome/getAppHomeConfigGoodsMustBuy?t=1663060275176&platform=5&channel=2')
			// console.log(result.data)
			commit('SET_BIGTRADEMARK',result.data)
		}catch(e){
			console.log(e)
		}
	},
	// 获取地图列表
	async getMapList({commit}){
		try{
			const result = await request('/index/homeFactory?t=1663062251236&platform=5&channel=2')
			// console.log(result.data)
			commit('SET_MAPLIST',result.data)
		}catch(e){
			console.log(e)
		}
	},
	// 获取首页产品列表
	async getHomeGoosList({commit}){
		try{
			const result = await request('/index/searchHomeFactoryGoods?factoryId=8&page=1&size=10&t=1663069171331&platform=5&channel=2')
			commit('SET_HOMEGOODSLIST',result.data)
		}catch(e){
			console.log(e)
		}
	}
	
}
const getters = {
	goodsInfo(state){
		return state.appreciine.goods || {}
	},
	tdGoods(state){
		return state.bigTrademark.goods || []
	},
	// 计算产品左侧列表
	goodsListL(state){
		return state.homeGoodsList.filter((item,index) => {
			if(index % 2 === 0) return item
		})
	},
	// 计算产品右侧列表
	goodsListR(state){
		return state.homeGoodsList.filter((item,index) => {
			if(index % 2 !== 0) return item
		})
	}
}

export default {
	namespaced:true,
	state,
	mutations,
	actions,
	getters
}